package at.ac.uniklu.rallyemaster.adapters;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import at.ac.uniklu.rallyemaster.RallyeMaster;
import at.ac.uniklu.rallyemaster.domain.Track;

public class DBTrack {
	private SQLiteDatabase db;
	private String[] columns;
	
	
	public DBTrack() throws SQLiteException{
		this.db = RallyeDBAdapter.getDatabase();
		if(db == null){
			RallyeMaster.logDebug("No Database exists");
		}
		columns = new String[]{DBConstants.KEY_TRACK_ID, DBConstants.KEY_TRACK_NAME, DBConstants.KEY_TRACK_NOTES};
	}
	
	/**
	 * Inserts a new Track to the database
	 * @param _track
	 * @return
	 */
	  public long insertTrack(Track _track) {
		  ContentValues newTrackValues = new ContentValues();      
		  newTrackValues.put(DBConstants.KEY_TRACK_NAME, _track.getName() ); 
		  newTrackValues.put(DBConstants.KEY_TRACK_NOTES, _track.getNotes() );
		  return db.insert(DBConstants.DATABASE_TABLE_TRACK, null, newTrackValues);
		  
	  }
	  
	  /**
	   * Removes a track based on his index
	   * @param id ID of the track which should be deleted
	   * @return
	   */
	  public boolean removeTrack(int id) {
	    return db.delete(DBConstants.DATABASE_TABLE_TRACK, DBConstants.KEY_TRACK_ID + "=" + id, null) > 0;
	  }
	  
	  /**
	   * Updates a track
	   * @param id ID of the track which should be updated
	   * @param TrackDescr
	   * @return
	   */
	  public boolean updateTrack(int id, Track track) {
	    ContentValues newValue = new ContentValues();
	    newValue.put(DBConstants.KEY_POINT_NOTES, track.getNotes());
	    newValue.put(DBConstants.KEY_TRACK_NAME, track.getName());
	    
	    return db.update(DBConstants.DATABASE_TABLE_TRACKINGPOINT, newValue, DBConstants.KEY_POINT_ID + "=" + id, null) > 0;
	  }
	 
	  
	  /**
	   * Get all Sights
	   **/
	  public ArrayList<Track> getAllTracks(){
		RallyeMaster.logDebug("Getting all tracks from db");
		Cursor result = db.query(DBConstants.DATABASE_TABLE_TRACK, columns, null, null, null, null, null);
		ArrayList<Track> tracks = fromCursorToTracks(result);
		return tracks;
	  }
	
	  
	  /**
	   * Converts from Cursor to a ArrayList of Tracks
	   * @param result
	   * @return
	   */
	private ArrayList<Track> fromCursorToTracks(Cursor result) {
		ArrayList<Track> tmpTrack = new ArrayList<Track>();
		
		result.moveToFirst();
		for(int i = 0; i<result.getCount(); i++){
			Track track = new Track();
			track.setId(result.getInt(result.getColumnIndex(DBConstants.KEY_TRACK_ID)));
			track.setName(result.getString(result.getColumnIndex(DBConstants.KEY_TRACK_NAME)));
			track.setNotes(result.getString(result.getColumnIndex(DBConstants.KEY_TRACK_NOTES)));
			tmpTrack.add(track);
			result.moveToNext();
		}
		return tmpTrack;
	}

	/**
	 * Return the last inserted track id
	 * @return
	 */
	public int getLastInsertedTrackID(){ 
		Cursor result = db.rawQuery("SELECT * from SQLITE_SEQUENCE;", null);
		result.moveToFirst();
		RallyeMaster.logDebug("count of rows in result cursor: "+ result.getCount());
		int rowID  = result.getInt(1);
			return rowID;	  
	}

	/**
	 * Returns a Track by his id
	 * @param id ID of the track which should be returned
	 * @return A track
	 */
	public Track getTrackByID(int id) {
		// TODO Auto-generated method stub
		Cursor result = db.query(DBConstants.DATABASE_TABLE_TRACK, columns, DBConstants.KEY_TRACK_ID +" = " + id, null, null, null, null);
		result.moveToFirst();
		Track track = new Track();
		if(result.getCount() == 1){
			track.setId(result.getInt(result.getColumnIndex(DBConstants.KEY_TRACK_ID)));
			track.setName(result.getString(result.getColumnIndex(DBConstants.KEY_TRACK_NAME)));
			track.setNotes(result.getString(result.getColumnIndex(DBConstants.KEY_TRACK_NOTES)));
		}else{
			return null;
		}
		return track;
	}

}
